<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" v-show="showSearch">
      <el-row :gutter="10">
        <el-col :span="6">
          <el-form-item label="小区名称" prop="communityId">
            <communitySelect v-model="queryParams.communityId" />
          </el-form-item>
        </el-col>
        <el-col :span="18">
          <el-form-item label-width="0">
            <el-button type="primary" icon="Search" @click="handleQuery"
              >搜索</el-button
            >
            <el-button type="primary" icon="Plus" @click="addHandle"
              >添加费用</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-table v-loading="loading" :data="chargeList" border>
      <el-table-column prop="id" label="ID" width="80" align="center" />
      <el-table-column prop="name" label="收费类型" align="center" />
      <el-table-column prop="communityName" label="适用小区" align="center" />
      <el-table-column prop="amount" label="单价" align="center" />
      <el-table-column prop="unit" label="单位" align="center" />
      <el-table-column
        prop="createTime"
        label="创建时间"
        width="180"
        align="center"
      />
      <el-table-column
        label="操作"
        width="180"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button type="primary" @click="editHandle(scope.row)"
            >编辑</el-button
          >
          <el-button type="danger" @click="deleteHandle(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <addEdit
      v-model="addDialog"
      v-if="addDialog"
      :title="title"
      :row="curRow"
      :communityId="queryParams.communityId"
      @submit="getList"
    />
  </div>
</template>

<script setup name="Post">
import { ref, getCurrentInstance } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import {
  getChargingStandardPage,
  removeChargingStandard,
} from "@/api/realEstateManage";
import communitySelect from "@/components/communitySelect";
import addEdit from "./addEdit.vue";
const { proxy } = getCurrentInstance();

const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const queryRef = ref();

const chargeList = ref([]); // 收费列表
const queryParams = ref({
  communityId: "",
  pageNo: 1,
  pageSize: 10,
});

const addDialog = ref(false);
const curRow = ref({});
const title = ref("");
const getList = () => {
  loading.value = true;
  getChargingStandardPage(queryParams.value)
    .then(({ data }) => {
      chargeList.value = data.records;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
};

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function addHandle() {
  addDialog.value = true;
  curRow.value = {};
  title.value = "添加收费项";
}

function editHandle(row) {
  addDialog.value = true;
  curRow.value = row;
  title.value = "编辑收费项";
}

function deleteHandle(row) {
  ElMessageBox.confirm("是否确认删除收费项？").then(() => {
    removeChargingStandard({ id: row.id }).then(({ data }) => {
      console.log("[ data ] >", data);
      if (data) {
        ElMessage({
          message: "删除成功",
          type: "success",
        });
        getList();
      } else {
        ElMessage({
          message: "删除失败",
          type: "error",
        });
      }
    });
  });
}

getList();
</script>

<style scoped lang="scss"></style>
